<?php

namespace app\shopapi\model\lakala;


use app\admin\model\Config;
use app\common\model\User;
use app\shopapi\model\Shop;
use fast\Http;

require_once ROOT_PATH.'extend/lakala/sdk/com/fds/FdsSdk.php';
//交易接口
class Pay
{
    public $appid = '6633965341557002240';

    public $DSAPublicKeyPath = ROOT_PATH.'extend/cert/cloud_dsa_public.pem';

    public $DSAPrivateKeyPath = ROOT_PATH.'extend/cert/dsa_private_key.pfx';

    public $RSAPublicKeyPath = ROOT_PATH.'extend/cert/cloud_rsa_public.pem';

    public $RSAPrivateKeyPath = ROOT_PATH.'extend/cert/rsa_private_key.pfx';

    public function __construct()
    {
        //应用编号，需修改
        \FdsSdk::setAppId($this->appid);

        \FdsSdk::setApiVersion("3.0");

        \FdsSdk::setDSAKey($this->DSAPublicKeyPath, $this->DSAPrivateKeyPath, "PXommGBW9gj0XWSq");

        \FdsSdk::setRSAKey($this->RSAPublicKeyPath, $this->RSAPrivateKeyPath, "uqouGAKgs4iuxOkm");
    }


    //商户提现
    public function withdraw($shop_id,$amount)
    {
        $shop_info = Shop::get($shop_id);
        \FdsSdk::setServerUrl("http://47.105.146.241:9510/service/soa");
        $memberInfo = \FdsSdk::getService()->memberMerchantInfo($shop_info);
        $out_order_no = date("YmdHis") . sprintf("%08d", $shop_id) . mt_rand(1000, 9999);
        $request = \think\Request::instance();
        $back_url = $request->root(true) . '/shopapi/auth/notify';
        $bank_card_no = '';
        $data = [
            'member_no'=>$memberInfo['member_no'],
            'out_order_no'=>$out_order_no,
            'amount'=>$amount,
            'back_url'=>$back_url,
            'billing_fee'=>0,
            'bank_card_no'=>$bank_card_no
        ];
        $result = \FdsSdk::getService()->withdraw($data);
        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }


    //公众号/服务窗/小程序支付下单,测试pay_method用QR_CODE
    public function orderConsume($data)
    {
        \FdsSdk::setServerUrl("http://47.105.146.241:9510/service/soa");

        $out_order_no = date("YmdHis") . sprintf("%08d", $data['shop_id']) . mt_rand(1000, 9999);

        $shop_info = Shop::get($data['shop_id']);

        $user_info = User::get($data['user_id']);

        $config = Config::get(1);
        $front_url = '';
        $front_fail_url = '';
        $data_param = [
            //'buyer_member_no'=>$buyer_member_no,
            //'seller_member_no'=>$data['seller_member_no'],
            'seller_member_no'=>6806043394058620928,
            'out_order_no'=>$out_order_no,
            'amount'=>$data['amount']*100,
            'terminal_ip'=>$data['terminal_ip'],
            'back_url'=>$data['back_url'],
            'order_name'=>$data['order_name'],
            'split_mode'=>$data['split_mode'],
            'pay_method'=>[
                /*'JSAPI'=>[
                    'amount'=>$amount*100,
                    'mch_appid'=>$config['mini_app_id'],
                    'openid'=>$user_info['wechat_open_id'],
                    'mode'=>'wxpay',
                    //'front_url'=>$front_url,
                    //'front_fail_url'=>$front_fail_url,
                    //'goods_detail'=>[],
                    'term_base_station'=>'',
                    'term_loc'=>'+'.$shop_info['lat'].'/-'.$shop_info['lng'],
                ],*/
                'GATEWAY'=>[
                    'amount'=>$data['amount']*100,
                    'merchant_no'=>'7194643510113705984',
                    'member_no'=>'6806043394058620928',
                    'product_name'=>$data['order_name'],
                    'support_refund'=>1,
                    'counter_param'=>[
                        'pay_mode'=>'ALIPAY'
                    ]
                ]
            ],

        ];
        if (isset($data['split_rule_data']))$data_param['split_rule_data'] = $data['split_rule_data'];
        //dump($data);
        $result = \FdsSdk::getService()->orderConsume($data_param);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }


    //退款
    public function refund($order_no,$out_request_no,$refund_amount,$pay_seq_no)
    {
        \FdsSdk::setServerUrl("http://47.105.146.241:9510/service/soa");

        $data = [
            'order_no'=>$order_no,
            'out_request_no'=>$out_request_no,
            'refund_amount'=>$refund_amount*100,
            'pay_seq_no'=>$pay_seq_no,
            'refund_rule'=>[
                [
                    'member_no'=>'6806043394058620928',
                    'amount'=>$refund_amount*100,
                ]
            ]
        ];

        $result = \FdsSdk::getService()->refund($data);
        if ($result['status']=='OK'){
            //return $result['result'];
            return [
                'code'=>1,
                'data'=>$result['result'],
                //'msg'=>$result['message']
            ];
        } else {
            return [
                'code'=>0,
                'msg'=>$result['message']
            ];
            //throw new \Exception($result['message']);
        }
    }

    //退款
    public function getPayStatus($order_no,$pay_seq_no)
    {
        \FdsSdk::setServerUrl("http://47.105.146.241:9510/service/soa");

        $data = [
            'order_no'=>$order_no,
            'pay_seq_no'=>$pay_seq_no,
        ];
        $result = \FdsSdk::getService()->getPayStatus($data);
        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //完成订单（确权分账）
    public function orderComplete($order_no,$member_no,$out_request_no,$split_amount)
    {
        \FdsSdk::setServerUrl("http://47.105.146.241:9510/service/soa");

        $data = [
            'order_no'=>$order_no,
            'member_no'=>$member_no,
            'out_request_no'=>$out_request_no,
            'split_amount'=>$split_amount
        ];
        //dump($data);
        $result = \FdsSdk::getService()->orderComplete($data);
        if ($result['status']=='OK'){
            //return $result['result'];
            return [
                'code'=>1,
                'data'=>$result['result'],
                //'msg'=>$result['message']
            ];
        } else {
            return [
                'code'=>0,
                'msg'=>$result['message']
            ];
            //throw new \Exception($result['message']);
        }
    }
}